package com.blockmeta.bbs.businesslibrary.widget.emoji;

import android.content.Context;
import android.content.res.Resources;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import android.widget.GridView;

import com.blockmeta.bbs.businesslibrary.R;
import com.vanniktech.emoji.VariantEmoji;
import com.vanniktech.emoji.emoji.EmojiCategory;

/**
 * @author AirThor
 * @date 2019/2/25.
 */
public class EmojiGridView extends GridView {
    public EmojiArrayAdapter emojiArrayAdapter;

    public EmojiGridView(final Context context) {
        super(context);

        final Resources resources = getResources();
        final int width = resources.getDimensionPixelSize(com.vanniktech.emoji.R.dimen.emoji_grid_view_column_width);
        final int spacing = resources.getDimensionPixelSize(com.vanniktech.emoji.R.dimen.emoji_grid_view_spacing);
        final int hSpacing = resources.getDimensionPixelSize(R.dimen._10dp);

        setColumnWidth(width);
        setHorizontalSpacing(hSpacing);
        setVerticalSpacing(spacing);
        setPadding(spacing, spacing, spacing, spacing);
        setNumColumns(7);
        setClipToPadding(false);
        setVerticalScrollBarEnabled(false);
    }

    public EmojiGridView init(@Nullable final OnEmojiClickListener onEmojiClickListener,
                              @Nullable final OnEmojiLongClickListener onEmojiLongClickListener,
                              @NonNull final EmojiCategory category, @NonNull final VariantEmoji variantManager) {
        emojiArrayAdapter = new EmojiArrayAdapter(getContext(), category.getEmojis(), variantManager,
                onEmojiClickListener, onEmojiLongClickListener);

        setAdapter(emojiArrayAdapter);

        return this;
    }

    @NonNull static <T> T checkNotNull(@Nullable final T reference, final String message) {
        if (reference == null) {
            throw new IllegalArgumentException(message);
        }

        return reference;
    }
}